<template>
  <div>
    <el-dialog
      :title="dialogtitle"
      width="1000px"
      class="icon-dialog"
      :visible.sync="show"
      :before-close="closeForm"
      append-to-body
      @open="open"
    >
      <el-form
        ref="form"
        :size="size"
        :model="form"
        :rules="rules"
        :label-width="$store.getters.device !== 'mobile'?'16%':'90px'"
      >

        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" auto-complete="off" clearable placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="使用门槛">
              <el-radio-group v-model="form.condition_type">
                <el-radio :label="0">无门槛</el-radio>
                <el-radio :label="1">有门槛</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.condition_type==1">
          <el-col :span="24">
            <el-form-item label="消费满金额">
              <el-input v-model="form.min_price" auto-complete="off" clearable placeholder="请输入消费满金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="适用范围">
              <el-radio-group v-model="form.use_goods">
                <el-radio :label="0">全部商品可用</el-radio>
                <el-radio :label="1">指定品类可用</el-radio>
                <el-radio :label="2">指定商品可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.use_goods==1">
          <el-col :span="24">
            <el-form-item label="商品分类" prop="cat_ids">
              <Treeselect
                v-model="form.cat_ids"
                :default-expand-level="1"
                :options="cat_idsarray"
                :normalizer="normalizer"
                :show-count="true"
                z-index="999999"
                placeholder="请选择所分类"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.use_goods==2">
          <el-col :span="24">
            <el-form-item label="指定商品">
              <div>
                <span v-if="form.goods"><el-image
                  v-if="form.goods.image"
                  class="goods_pic"
                  :src="form.goods.image"
                  :preview-src-list="[form.goods.image]"
                /></span> <el-button size="mini" type="primary" plain @click="showSelectGoods = true">选择商品</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="使用计算" prop="equal">
              <el-input-number
                v-model="form.equal"
                controls-position="right"
                style="width:300px;"
                auto-complete="off"
                clearable
                :min="0"
                placeholder="使用计算"
              />
              <div class="help-block">多少积分可以抵扣1元</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序">
              <el-input-number
                v-model="form.sort"
                controls-position="right"
                style="width:200px;"
                auto-complete="off"
                clearable
                :min="0"
                placeholder="排序"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">保 存</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
    <!--添加商品弹窗-->
    <SelectGoods :show.sync="showSelectGoods" size="mini" />
  </div>
</template>
<script>
  import SelectGoods from '@/views/memberaccounttype/selectGoods.vue'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'memberaccounttypeupdate',
  components: {Treeselect,SelectGoods},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    opentype: {
      type: String,
      default: 'add'
    },
    size: {
      type: String,
      default: 'small'
    },
    info: {
      type: Object
    }
  },
  data() {
    return {
      lang: {},
      dialogtitle: '',
      form: {
        title: '',
        status: 1
      },
      colorarray: [],
      ptypearray: [],
      expire_typearray: [],
      cat_idsarray: [],
      loading: false,
      showSelectGoods: false,
      rules: {
        name: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.$api.post('/memberaccounttype/getField').then(res => {
          this.colorarray = res.data.colorarray
          this.ptypearray = res.data.ptypearray
          this.expire_typearray = res.data.expire_typearray
          this.cat_idsarray = res.data.cat_idsarray
        })
      }
    }
  },
  mounted() {
    this.$api.post('/Lang/getlang').then(res => {
      this.lang = res.data
    })
  },
  methods: {
    open() {
      if (this.opentype == 'update') {
        this.dialogtitle = '修改'
        this.form = this.info
      } else {
        this.dialogtitle = '添加'
        this.form = {
          goods_ids: 0,
          goods: [],
          expire_type: 10,
          sort: 100,
          status: 1
        }
        // console.log(this.form);
      }
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$api.post('/memberaccounttype/update', this.form).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$emit('refesh_list')
            this.closeForm()
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    closeForm() {
      this.$emit('update:show', false)
      this.loading = false
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
    },
    normalizer(data) {
      if (data.children && !data.children.length) {
        delete data.children
      }
      return {
        id: data.val,
        label: data.key,
        children: data.children
      }
    }
  }
}
</script>
<style scoped>
  .goods_pic {
    width: 100px;
    vertical-align: middle;
  }
</style>
